//
//  SendStatusController.swift
//  微博项目
//
//  Created by zhang on 16/3/13.
//  Copyright © 2016年 jin. All rights reserved.
//

import UIKit

class SendStatusController: UIViewController,UITextViewDelegate,SendStatusToolViewDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate {
    weak var textView:StatusTextView?
    weak var toolView:UIView?
    weak var photoView:UIImageView?
    var photoURL:NSURL?
    override func viewDidLoad() {
        super.viewDidLoad()
        self.setNavigationButton()
        // 设置文本框
        let textView = StatusTextView(frame: CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height))
        textView.delegate = self
        self.view.addSubview(textView)
        self.textView = textView
        // 设置工具条
        let toolView = SendStatusToolView()
        toolView.sendStatusToolViewDelegate = self
        self.view.addSubview(toolView)
        self.toolView = toolView
        // 设置图片
        let photoView = UIImageView()
        photoView.frame = CGRectMake(0, 150, 200, 200)
        self.view.addSubview(photoView)
        self.photoView = photoView
        // 监听文本框的改变
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "textChange", name: UITextViewTextDidChangeNotification, object: textView)
        // 监听文本框的改变
        NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardChangeFrame:"), name: UIKeyboardWillChangeFrameNotification, object:nil)
        // Do any additional setup after loading the view.
    }
    func setNavigationButton()
    {
        self.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "取消", style: UIBarButtonItemStyle.Plain, target: self, action: "close")
        
        let button = UIButton(type: UIButtonType.Custom)
        button.setTitle("发送", forState: UIControlState.Normal)
        button.sizeToFit()
        button.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        button.setTitleColor(UIColor.grayColor(), forState: UIControlState.Disabled)
        button.addTarget(self, action: "sendStatus", forControlEvents: UIControlEvents.TouchDown)
        self.navigationItem.rightBarButtonItem = UIBarButtonItem(customView: button)
        self.navigationItem.rightBarButtonItem?.enabled = false
    }
    func sendStatus()
    {
        let hud = MBProgressHUD.showHUDAddedTo(self.view, animated: true)
        hud.labelText = "正在发送。。。。"
        SendStatusTool.send((self.textView?.text)!, image: self.photoView!.image!, success: { () -> (Void) in
                hud.hide(true)
                self.dismissViewControllerAnimated(true, completion: nil)
            }) { () -> (Void) in
                hud.labelText = "发送失败。。。。"
                hud.hide(true, afterDelay: 2)
        }
    }
    func keyboardChangeFrame(notification:NSNotification)
    {
        /**
        {name = UIKeyboardWillChangeFrameNotification; userInfo = {
        UIKeyboardAnimationCurveUserInfoKey = 7;
        UIKeyboardAnimationDurationUserInfoKey = "0.25";
        UIKeyboardBoundsUserInfoKey = "NSRect: {{0, 0}, {375, 258}}";
        UIKeyboardCenterBeginUserInfoKey = "NSPoint: {187.5, 796}";
        UIKeyboardCenterEndUserInfoKey = "NSPoint: {187.5, 538}";
        UIKeyboardFrameBeginUserInfoKey = "NSRect: {{0, 667}, {375, 258}}";
        UIKeyboardFrameEndUserInfoKey = "NSRect: {{0, 409}, {375, 258}}";
        UIKeyboardIsLocalUserInfoKey = 1;
        }}
        */
        let beginPoint:CGPoint = (notification.userInfo!["UIKeyboardCenterBeginUserInfoKey"]?.CGPointValue)!
        let endPoint:CGPoint = (notification.userInfo!["UIKeyboardCenterEndUserInfoKey"]?.CGPointValue)!
        let duration:NSTimeInterval = notification.userInfo!["UIKeyboardAnimationDurationUserInfoKey"] as! NSTimeInterval
        UIView.animateWithDuration(duration) { [weak self] () -> Void in
            if (endPoint.y - beginPoint.y) < 0
            {
                self!.toolView?.transform = CGAffineTransformMakeTranslation(0, (endPoint.y - beginPoint.y))
            }
            else
            {
                self!.toolView?.transform = CGAffineTransformIdentity
            }
        }
    }
    deinit
    {
        // 移除
        NSNotificationCenter.defaultCenter().removeObserver(self)
    }
    // 文本发生变化的时候
    func textChange()
    {
        let textLength:Int = (self.textView?.text.lengthOfBytesUsingEncoding(NSUTF8StringEncoding))!
        if textLength > 0
        {
            self.textView?.placeholderText = ""
            self.navigationItem.rightBarButtonItem?.enabled = true
        }
        else
        {
            self.textView?.placeholderText = "说点什么吧"
            self.navigationItem.rightBarButtonItem?.enabled = false
        }
    }
    // 拖动的时候
    func scrollViewDidScroll(scrollView: UIScrollView) {
        self.view.endEditing(true)
    }
    // 关闭
    func close()
    {
        self.view.endEditing(true)
        self.dismissViewControllerAnimated(true, completion: nil)
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    func sendStatusToolViewPictureButtonClick() {
        self.toolView?.transform = CGAffineTransformIdentity
        // 弹出系统的相册
        let imagePicker = UIImagePickerController()
        
        // 设置相册类型,相册集
        imagePicker.sourceType = UIImagePickerControllerSourceType.SavedPhotosAlbum
        
        imagePicker.delegate = self;
        self.presentViewController(imagePicker, animated: true, completion: nil)
    }
    func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
        self.photoView?.image = info["UIImagePickerControllerOriginalImage"] as! UIImage
        picker.dismissViewControllerAnimated(true, completion: nil)
    }
    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
